package com.tbsoft.ehr.action.sysdata;

import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.opensymphony.xwork2.ActionContext;
import com.tbsoft.ehr.action.BaseAction;
import com.tbsoft.ehr.model.PublicField;
import com.tbsoft.ehr.service.pubField.PubFieldService;

/**
 * @author lizhenhua
 * 
 */
public class PubfieldAction extends BaseAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private static final Log log = LogFactory.getLog(PubfieldAction.class);

	private PubFieldService pubFieldService;

	private List<PublicField> pubFieldLst;
	private PublicField pf;

	public List<PublicField> getPubFieldLst() {
		return pubFieldLst;
	}

	public void setPubFieldLst(List<PublicField> pubFieldLst) {
		this.pubFieldLst = pubFieldLst;
	}

	public PublicField getPf() {
		return pf;
	}

	public void setPf(PublicField pf) {
		this.pf = pf;
	}

	public PubFieldService getPubFieldService() {
		return pubFieldService;
	}

	public void setPubFieldService(PubFieldService pubFieldService) {
		this.pubFieldService = pubFieldService;
	}

	public String query() {
		if (this.pf.getFieldType().length() > 0) {
			this.pubFieldLst = this.pubFieldService.getPubFieldByType(this.pf
					.getFieldType());
		}
		return "browse";
	}

	public String save() {
		try {
			Map<String, String> fieldTypeMap = (Map) ActionContext.getContext()
					.getApplication().get("PUBFIELDTYPE_MAP");
			String remark = fieldTypeMap.get(this.pf.getFieldType());
			this.pf.setRemark(remark);
			this.pubFieldService.addPubField(this.pf);
			ActionContext.getContext().getApplication().put("PUBFIELD_MAP",
					this.pubFieldService.getPubFieldMap());
			setRel("pubfield_browse");
			return SUCCESS;
		} catch (Exception ex) {
			log.error("添加公共字段失败", ex);
			return errorReturn("添加公共字段失败:" + ex.getMessage());
		}
	} 
	
	public String delete(){
		try{
			this.pubFieldService.deletePubField(this.pf);
			ActionContext.getContext().getApplication().put("PUBFIELD_MAP", this.pubFieldService.getPubFieldMap());
			ActionContext.getContext().getApplication().put("PUBFIELDTYPE_MAP", this.pubFieldService.getPubFieldTypeMap());
			return SUCCESS;
		} catch(Exception ex){
			log.error("删除公共字段失败",ex);
			return errorReturn("删除公共字段失败:"+ex.getMessage());
		}
			
	}
	
	public String edit(){
		this.pf = this.pubFieldService.getPubField(this.pf);
		return "edit";
	}
	
	public String update(){
		try{			
			this.pubFieldService.updatePubField(this.pf);
			ActionContext.getContext().getApplication().put("PUBFIELD_MAP", this.pubFieldService.getPubFieldMap());
			setRel("pubfield_browse");
			return SUCCESS;
		}catch(Exception ex){
			log.error("更新公共字段失败",ex);
			return errorReturn("更新公共字段失败:"+ex.getMessage());
		}
		
	}
}
